<template>
	<view class="content">
		<view class="top_tab">
			<view class="top_tab_wrap">
				<view class="tab_item" v-for="(item, index) in top_tab" :key="index">
					<view :class="currentTab == index ? 'item_active' : 'item_name'" @click="changeTab(index)">{{ item.tab_name }}</view>
				</view>
			</view>
			<view class="top_tab_right" @click="toTransferRecord"><image src="https://jialianxin-1301053941.cos.ap-chengdu.myqcloud.com/static/order.png" mode="aspectFit"></image></view>
		</view>

		<!-- 选择划拨内容111 -->
		<view class="choose_transfer_box" v-if="currentTab == 0">
			<view class="choose_transfer">
				<view class="choose_transfer_wrap">
					<view class="choose_transfer_left">选择需要划拨的设备（多选）</view>
					<view class="choose_transfer_middle">
						可划拨的设备
						<text>{{ total }}</text>
						台
					</view>
					<view class="choose_transfer_right" @tap="open(2, 'right')">筛选</view>
					<openAlert ref="openAlert" :shadeClose="true" :AlertClass="AlertClass" :AlertPosition="AlertPosition">
						<view class="sreening_wrap">
							<view class="type_obj_str" v-for="(item, index) in type_obj" :key="index" @click="select_load(item)">{{ item.name }}</view>
						</view>
					</openAlert>
				</view>
			</view>
		
			<view class="card_list_wrap">
				<checkbox-group @change="changeCheckbox">
					<!-- <empty v-if="loaded === true && checkboxData.length === 0"></empty> -->
					<label class="card_item" v-for="(item, index) in checkboxData" :key="index">
						<view class="card_list">
							<view class="card_list_left">
								<view class="card_name">{{ item.activestatus_text }}</view>
								<view class="card_num">
									<text selectable="true">{{ item.sn }}</text>
								</view>
							</view>
							<checkbox
								:value="String(item.id)"
								:checked="checkedArr.includes(String(item.id))"
								:class="{ checked: checkedArr.includes(String(item.id)) }"
								color="#0086ff"
								style="transform:scale(0.7);"
							/>
						</view>
					</label>
					<uni-load-more :status="loadingType"></uni-load-more>
					<view class="blank"></view>
				</checkbox-group>
			</view>

			<view class="Transfer_bottom">
				<view class="Transfer_bottom_wrap">
					<view class="Transfer_bottom_left">
						<checkbox-group @change="allChoose">
							<label>
								<checkbox value="all" :class="{ checked: allChecked }" :checked="allChecked ? true : false" color="#0086ff" style="transform:scale(0.9);" />
								<text>全选</text>
							</label>
						</checkbox-group>
					</view>
					<view class="Transfer_bottom_right">
						<view class="total">
							总计:
							<text>{{ num }}台</text>
						</view>
						<view class="Transfer_btn_wrap"><view class="Transfer_btn" @click="toTransferPeople">划拨给...</view></view>
					</view>
				</view>
			</view>
		</view>

		<!-- 区间划拨内容 -->
		<view class="interval_ransfer_box" v-if="currentTab == 1">
			<form @submit="submitForm">
				<view class="interval_ransfer">
					<view class="interval_ransfer_title">
						<view class="ransfer_title_left">选择需要划拨的区间</view>
						<view class="ransfer_title_right">
							可划拨的设备
							<text>{{ total }}</text>
							台
						</view>
					</view>

					<view class="interval_ransfer_input">
						<view class="ransfer_input_left">起始序列号</view>
						<view class="ransfer_input_right"><input type="text" :value="startend" name="startend" @input="startends" /></view>
					</view>

					<view class="interval_ransfer_input">
						<view class="ransfer_input_left">划拨数量</view>
						<view class="ransfer_input_right"><input type="number" :value="trnum" maxlength="1000" name="trnume" @input="trnume" /></view>
					</view>

					<view class="interval_ransfer_input">
						<view class="ransfer_input_left">截止序列号</view>
						<view class="ransfer_input_right"><input type="text" disabled="true" :value="endcode" /></view>
					</view>

					<view class="interval_ransfer_input">
						<view class="ransfer_input_left">备注</view>
						<scroll-view scroll-y="true" class="ransfer_input_right1"><textarea maxlength="-1" :value="remark" placeholder="" /></scroll-view>
					</view>
				</view>
				<view class="interval_ransfer_bottom">
					<view class="interval_ransfer_btn">
						划拨给...
						<button form-type="submit"></button>
					</view>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
import openAlert from '../../components/open-alert/open-alert.vue';
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import empty from '@/components/empty';
export default {
	components: { openAlert, uniLoadMore, empty },
	data() {
		return {
			ids: [],
			total: 0,
			startend: '',
			endcode: '',
			remark: '',
			trnum: '',
			num: 0,
			checkedArr: [], //复选框选中的值
			allChecked: false, //是否全选
			AlertClass: 0,
			AlertPosition: '',
			currentTab: 0,
			top_tab: [
				{
					tab_name: '选择划拨'
				},
				{
					tab_name: '区间划拨'
				}
			],

			loaded: false,
			loadingType: 'more',
			pageNum: 1,
			limit: 10,
			type_id: 0,
			type_obj: [],
			checkboxData: [
				// {
				// 	card_name:'洁牙卡1-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡3-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡3-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡4-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡5-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡6-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡7-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡8-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡9-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡10-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡11-卡号',
				// 	card_num:'013404'
				// },{
				// 	card_name:'洁牙卡12-卡号',
				// 	card_num:'013404'
				// }
			],
			token:'',
		
		};
	},
	onShow() {
		//显示内容
		this.loadData(0);
		//获取类型列表
		// var userInfo = uni.getStorageSync('userInfo');
		let params = {
			page: this.pageNum,
			limit: this.limit,
			category_id: this.type_id,
			token:this.token
		};
		// GET/api/usercenter/device/category
		var url = '/api/usercenter/device/category';
		this.Unfold_data.upload_data(params, 'GET', url, res => {
			if (res.statusCode == 200 && res.data.code == 1) {
				this.type_obj = res.data.data;
			} else {
				this.Unfold_data.showWindow(res.data.msg);
			}
		});
	},
	onLoad(options){
		this.token=options.token
		// this.shop_id=options.shop_id,
		
	},
	
	
	onReachBottom() {
		this.loadData(1);
	},
	methods: {
		//选择pos机类型
		select_load(item) {
			this.type_id = item.id;
			this.pageNum = 1
			this.loadData(3);
		},
		//分页获取好啵设备
		loadData(source) {
			//source 防止重复加载
			if (source == 0 && this.loaded) {
				return;
			}
			if (source == 1 && this.loadingType != 'more') {
				return;
			}
			if (source == 3) {
				this.checkboxData = [];
			}
            // let userInfo = uni.getStorageSync('userInfo');
			let params = {};
			if (this.type_id == 0) {
				params = {
					page: this.pageNum,
					limit: this.limit,
					token:this.token
				};
			} else {
				params = {
					page: this.pageNum,
					limit: this.limit,
					category_id: this.type_id,
					token:userInfo.token
				};
			}
			var url = '/api/usercenter/device';
			this.Unfold_data.upload_data(params, 'GET', url, res => {
				if (res.statusCode == 200 && res.data.code == 1) {
					//赋值
					this.endcode = res.data.data.sn;
					this.total = res.data.data.total;
					if (source == 0) {
						this.loaded = true;
					}
					if (!res.data.data.rows) {
						this.loadingType = 'noMore';
					} else if (res.data.data.rows.length < this.limit) {
						this.loadingType = 'noMore';
						if (res.data.data.rows.length > 0) {
							this.checkboxData = this.checkboxData.concat(res.data.data.rows);
						}
					} else if (res.data.data.rows.length == this.limit) {
						if (res.data.data.rows.length > 0) {
							this.checkboxData = this.checkboxData.concat(res.data.data.rows);
						}
						this.pageNum = this.pageNum + 1;
					}
					this.$refs.openAlert.Close();
				} else if (res.statusCode == 200 && res.data.code == 401) {
					this.Unfold_data.showWindow(res.data.msg);
				} else {
					this.Unfold_data.showWindow(res.data.msg);
				}
			});
		},
		startends(e) {
			// console.log(e);
			this.startend = e.detail.value;
		},
		//监听划拨
		trnume(a) {
			// console.log();
			// let userInfo = uni.getStorageSync('userInfo');
			let params = {
				// T文本	是
				sn: this.startend,
				//起始编号
				num: Number(a.detail.value),
				token:this.token
			};
			var url = '/api/usercenter/device/endSn';
			this.Unfold_data.upload_data(params, 'POST', url, res => {
				if (res.statusCode == 200 && res.data.code == 1) {
					//获取截至编号
					this.endcode = res.data.data.sn;
				} else if (res.statusCode == 200 && res.data.code == 401) {
					this.Unfold_data.showWindow(res.data.msg);
				} else {
					this.Unfold_data.showWindow(res.data.msg);
				}
			});
		},
		changeTab(index) {
			this.currentTab = index;
		},
		// 多选复选框改变事件
		changeCheckbox(e) {
			this.checkedArr = e.detail.value;

			this.num = this.checkedArr.length;
			this.ids = this.checkedArr;

			// 如果选择的数组中有值，并且长度等于列表的长度，就是全选
			if (this.checkedArr.length > 0 && this.checkedArr.length == this.checkboxData.length) {
				this.allChecked = true;
			} else {
				this.allChecked = false;
			}
		},
		// 全选事件
		allChoose(e) {
			let chooseItem = e.detail.value;
			// 全选
			if (chooseItem[0] == 'all') {
				this.num = this.checkboxData.length;
				this.allChecked = true;
				for (let item of this.checkboxData) {
					let itemVal = String(item.id);
					if (!this.checkedArr.includes(itemVal)) {
						this.checkedArr.push(itemVal);
					}
				}
			} else {
				// 取消全选
				this.allChecked = false;
				this.checkedArr = [];
				if (this.checkedArr) {
					this.num = 0;
				}
			}
		},
		submitForm(e) {
			//区间划拨
			if (!e.detail.value.startend) {
				this.Unfold_data.showWindow('起始序列号不能为空');
				return;
			}
			if (!e.detail.value.endcode) {
				this.Unfold_data.showWindow('结束序列号不能为空');
				return;
			}
			if (!e.detail.value.trnume) {
				this.Unfold_data.showWindow('划拨数量不能为空');
				return;
			}

			let params = {
				//开始
				sn: this.startend,
				//结束
				end: this.endcode,
				//划拨数量
				num: e.detail.value.trnume
			};
			uni.setStorageSync('par', params);
			uni.navigateTo({
				url: '/pages/transferPeople/transferPeople'
			});
		},
		toTransferRecord() {
			uni.navigateTo({
				url: '/pages/transferRecord/transferRecord'
			});
		},
		toTransferPeople() {
			if (this.ids=='') {
				this.Unfold_data.showWindow('请选择设备');
				return;
			}
			uni.navigateTo({
				url: '/pages/transferPeople/transferPeople?ids=' + this.ids
			});
		},
		//打开弹出层
		open(Class, Position) {
			this.$nextTick(function() {
				this.AlertClass = Class;
				this.AlertPosition = Position;
				this.$nextTick(function() {
					this.$refs.openAlert.Show();
				});
			});
		}
	}
};
</script>

<style lang="scss">
page {
	width: 100%;
	height: 100%;
	background-color: #f5f9fc;
}
.blank {
	height: 100upx;
}
.content {
	width: 100%;
	position: relative;
	padding-bottom: 100upx;
	.top_tab {
		width: 100%;
		height: 100upx;
		margin-top: 30px;
		border-top: 6upx solid #dadada;
		position: fixed;
		left: 0;
		top: 20upx;
		z-index: 10;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: center;
		// margin-top: 86upx;
		.top_tab_wrap {
			flex: 9;
			display: flex;
			align-items: center;
			justify-content: center;
			.tab_item {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
				.item_name {
					padding: 20upx 0;
					border-bottom: 6upx solid #ffffff;
				}
				.item_active {
					padding: 20upx 0;
					border-bottom: 6upx solid #1a80da;
					color: #1a80da;
				}
			}
		}
		.top_tab_right {
			flex: 1;
			image {
				width: 60upx;
				height: 60upx;
			}
		}
	}
	.choose_transfer_box {
		width: 100%;
		margin-top: 130upx;
		.choose_transfer {
			width: 100%;
			height: 90upx;
			margin-top: 30px;
			display: flex;
			align-items: center;
			justify-content: center;
			.choose_transfer_wrap {
				width: 95%;
				margin: 0 auto;
				display: flex;
				align-items: center;
				font-size: 26upx;
				position: fixed;
				height: 85px;
		        background-color: #f5f9fc;
		        width: 100%;
		        z-index: 100;				
				.choose_transfer_left {
					background: #ffffff;
					padding: 10upx 15upx;
					border-radius: 30upx;
				}
				.choose_transfer_middle {
					padding: 0 30upx;
					text {
						color: #1a80da;
					}
				}
				.choose_transfer_right {
					text-align: right;
					color: #1a80da;
				}
				.sreening_wrap {
					width: 500upx;
					height: 100vh;
					background-color: #ffffff;
					.type_obj_str {
						height: 100upx;
						line-height: 100upx;
						text-align: center;
						font-size: 36upx;
						color: #7c7c7c;
						border-bottom: 2upx solid #c0c4cc;
					}
				}
			}
		}
		
		
		.card_list_wrap {
			width: 100%;
			height: 100vh;
			margin-top: 20px;
			
			.card_item {
				width: 100%;
				height: 120upx;
				background-color: #ffffff;
				border-bottom: 2upx solid #ededed;
				display: flex;
				align-items: center;
				justify-content: center;
				.card_list {
					width: 90%;
					margin: 0 auto;
					display: flex;
					align-items: center;
					.card_list_left {
						flex: 3;
						.card_name {
							font-size: 24upx;
							padding: 10upx 0;
						}
						.card_num {
							font-size: 36upx;
						}
					}
					.checkbox {
						flex: 1;
						text-align: right;
					}
				}
			}
		}
		.Transfer_bottom {
			width: 100%;
			height: 100upx;
			background: #ffffff;
			position: fixed;
			left: 0;
			bottom: 0;
			.Transfer_bottom_wrap {
				width: 95%;
				margin: 0 auto;
				height: 100upx;
				display: flex;
				align-items: center;
				justify-content: center;
				.Transfer_bottom_left {
					flex: 1;
				}
				.Transfer_bottom_right {
					flex: 2;
					display: flex;
					align-items: center;
					justify-content: center;
					.total {
						flex: 1;
						display: flex;
						align-items: center;
						justify-content: flex-end;
						text {
							padding-left: 15upx;
						}
					}
					.Transfer_btn_wrap {
						flex: 1;
						width: 100%;
					}
					.Transfer_btn {
						width: 75%;
						margin: 0 auto;
						height: 70upx;
						background-color: #8973f9;
						border-radius: 40upx;
						display: flex;
						align-items: center;
						justify-content: center;
						color: #ffffff;
					}
				}
			}
		}
	}
	.interval_ransfer_box {
		width: 100%;
		margin-top: 120upx;
		background: #ffffff;
		.interval_ransfer {
			width: 95%;
			margin: 0 auto;
			padding: 20upx 0;
			.interval_ransfer_title {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 26upx;
				padding-bottom: 30upx;
				.ransfer_title_left {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: center;
				}
				.ransfer_title_right {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: center;
					text {
						color: #1a80da;
						padding: 0 10upx;
					}
				}
			}
			.interval_ransfer_input {
				width: 100%;
				padding: 20upx;
				display: flex;
				align-items: center;
				justify-content: center;
				.ransfer_input_left {
					flex: 1;
				}
				.ransfer_input_right {
					flex: 3;
					width: 100%;
					height: 80upx;
					background: #f5f9fc;
					display: flex;
					align-items: center;
					input {
						padding-left: 20upx;
					}
				}
				.ransfer_input_right1 {
					flex: 3;
					width: 100%;
					height: 200upx;
					background: #f5f9fc;
					display: flex;
					align-items: center;
					textarea {
						width: 100%;
						height: 200upx;
						padding-left: 20upx;
					}
				}
			}
		}
		.interval_ransfer_bottom {
			width: 100%;
			height: 100upx;
			background: #ffffff;
			position: fixed;
			left: 0;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			padding-right: 40upx;
			.interval_ransfer_btn {
				width: 180upx;
				height: 70upx;
				background-color: #8973f9;
				border-radius: 40upx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #ffffff;
				position: relative;
				overflow: hidden;
				button {
					position: absolute;
					left: 0;
					top: 0;
					width: 180upx;
					height: 70upx;
					opacity: 0;
				}
			}
		}
	}
}
</style>
